package com.ykt.controller;

import com.jfinal.aop.Before;
import com.ykt.common.utils.Colls;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;
import com.ykt.converter.AgentFeeConverter;
import com.ykt.converter.CommunityConverter;
import com.ykt.model.AgentFee;
import com.ykt.model.Community;
import com.ykt.service.AgentFeeService;
import com.ykt.service.CommunityService;
import com.ykt.validator.CommonValidator;
import com.ykt.vo.AgentFeeVo;
import com.ykt.vo.CommunityVo;
import com.ykt.vo.ResultVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社区点查询操作
 *
 * @author wing
 * @version 1.0.0
 */
public class CommunityController extends BaseController {

    private AgentFeeService agentFeeService = enhance(AgentFeeService.class.getName(), AgentFeeService.class);
    private CommunityService communityService = enhance(CommunityService.class.getName(), CommunityService.class);


    @Before(CommonValidator.LonLatValidator.class)
    public void find() {
        Integer userId = getParaToInt("userId", -1);
        Integer shopId = getParaToInt("shopId", -1);
        String longitude = getPara("longitude", "0");
        String latitude = getPara("latitude", "0");

        Map<String, Object> paramMap = new HashMap<>();
        //paramMap.put("shopId", shopId);
        paramMap.put("longitude", Double.valueOf(longitude));
        paramMap.put("latitude", Double.valueOf(latitude));

        PageParam<Map<String, Object>> param = super.toPageParam(paramMap);
        PageData<Community> page = communityService.paginate(param);
        if(page.getPageData() == null){
            page.setPageData(new ArrayList<Community>());
        }

        // 转换和设置代购费
        PageData<CommunityVo> pageVo = page.convert(CommunityConverter.convert(page.getPageData()));
        this.setAgentFee(pageVo.getPageData());

        // 返回数据
        this.returnJson(ResultVo.success(pageVo));
    }

    private void setAgentFee(List<CommunityVo> vos){
        List<Integer> communityIds = new ArrayList<>();
        for (CommunityVo vo : vos) {
            communityIds.add(vo.getId());
        }

        List<AgentFee> agentFees = agentFeeService.findByCommunityIds(communityIds);
        if(Colls.isEmpty(agentFees)){
            return ;
        }

        List<AgentFeeVo> agentFeeVos = AgentFeeConverter.convertList(agentFees);
        if(Colls.isEmpty(agentFeeVos)){
            return ;
        }

        Map<Integer, List<AgentFeeVo>> groupMap = new HashMap<>();
        for (AgentFeeVo agentFeeVo : agentFeeVos) {

            if(!groupMap.containsKey(agentFeeVo.getCommunityId())){
                groupMap.put(agentFeeVo.getCommunityId(), new ArrayList<AgentFeeVo>());
            }

            groupMap.get(agentFeeVo.getCommunityId()).add(agentFeeVo);
        }

        for (CommunityVo communityVo : vos) {
            communityVo.setAgentFee(groupMap.get(communityVo.getId()));
        }
    }
}
